<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>需求管理</title>
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <meta http-equiv="Cache-Control" content="no-siteapp"/>
    <script src="/layui/layui.js" charset="utf-8"></script>
    <link rel="stylesheet" href="/layui/css/layui.css">
    <script src="/js/jquery-3.5.1.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    <style>
        .layui-table-tool{
            background-color: rgb(199 189 189 / 23%);
        }
    </style>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row">
        <div class="layui-col-md8" style="padding-top: 12px;">
        </div>
        <div class="layui-col-md4">
            <a class="layui-btn layui-btn-small" style="line-height:1.6em;float:right"
               href="javascript:location.replace(location.href);" title="刷新">
                <i class="layui-icon layui-icon-refresh
" style="line-height:30px"></i></a>
        </div>
    </div>
</div>
<div class="layui-container">
    <form class="layui-form" action="" id="addDemandForm">
        <div class="layui-row" style="margin-top: 10px;">
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">所属项目:</label>
                    <div class="layui-input-block">
                        <select name="project" lay-verify="required" lay-reqtext="请选择项目" lay-search="">
                            <option value="">直接选择或搜索选择</option>
                            <option th:each="list:${listProject}" th:value="${list.getProjectName()}"
                                    th:text="${list.getProjectName()}"></option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">需求日期:</label>
                    <div class="layui-input-block">
                        <input type="text" name="date" id="date" lay-verify="date" placeholder="yyyy-MM-dd"
                               autocomplete="off"
                               class="layui-input">
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-row">
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">版本号:</label>
                    <div class="layui-input-block">
                        <input type="text" name="version" lay-verify="required" lay-reqtext="版本号是必填项"
                               placeholder="请输入版本号"
                               autocomplete="off" class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">状态:</label>
                    <div class="layui-input-block">
                        <select name="demandStatus" lay-verify="required" lay-reqtext="请选择状态">
                            <option value="" selected=""></option>
                            <option value="0">未完成</option>
                            <option value="1">已完成</option>
                        </select>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-row">
            <div class="layui-col-md12">
                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label">需求概述:</label>
                    <div class="layui-input-block">
                        <textarea placeholder="请输入内容" name="context" class="layui-textarea"></textarea>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-row">
            <div class="layui-col-md12">
                <fieldset class="layui-elem-field layui-field-title">
                    <legend>请选择归属标签:</legend>
                </fieldset>
                <div id="test4" class="demo-transfer"></div>
            </div>
        </div>
        <div class="layui-row">
            <div class="layui-col-md12">
                <div class="layui-form-item">
                    <div class="layui-input-block" style="margin-left: 0px;margin-top: 20px">
                        <button type="submit" class="layui-btn" lay-submit="" lay-filter="demo1"
                                lay-demotransferactive="reload">立即提交
                        </button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </div>
        </div>
    </form>
</div>
<div class="layui-fluid">
    <div class="layui-row">
        <table class="layui-hide" id="table" lay-filter="table"></table>
    </div>
</div>

<script>
    layui.use(['form', 'layedit',"table", 'laydate', 'layer', 'util', 'transfer'], function () {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate
            , transfer = layui.transfer
            , util = layui.util,
            table = layui.table;

        //渲染
        $.get('/demand/getLabelData', function (result) {
            //显示搜索框
            transfer.render({
                elem: '#test4', //绑定元素
                title: ['未选择', '已选择'],
                showSearch: true,//开启搜索框
                data: result.data,
                id: 'transfer_data', //定义索引
                parseData: function (res) {
                    return {
                        "value": res.id ////穿梭框的唯一标识
                        , "title": res.labelName //穿梭框的值
                        // , "checked": '' //是否选中
                    }
                },
            })
        })
        //固定块
        util.fixbar({
            css: {right: 50, bottom: 100}
            , bgcolor: '#393D49'
            , click: function (type) {
                console.log(type);
                if (type === 'top') {
                    layer.msg('返回顶部')
                }
            }
        })

        // 设置最大可选的日期
        function maxDate() {
            var now = new Date();
            return now.getFullYear() + "-" + (now.getMonth() + 1) + "-" + now.getDate();
        }

        //日期
        laydate.render({
            elem: '#date',
            max: maxDate()
        });
        //自定义验证规则
        form.verify({
            title: function (value) {
                if (value.length < 5) {
                    return '标题至少得5个字符啊';
                }
            }
            , content: function (value) {
                layedit.sync(editIndex);
            }
        });
        layui.use('table', function () {
            var table = layui.table;
            table.render({
                elem: '#table'
                , url: 'getListData'
                , method: 'GET'
                , title: '需求录入表'
                , toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
                text: {
                    none: '暂无相关数据' //默认：无数据。
                }
                , limit: 15
                , limits: [15, 30, 45, 60, 75, 90]
                , loading: true //是否开始加载条
                , even: true, //开启隔行背景#
                skin: 'line' //行边框风格
                , cols:
                    [
                        [
                            // {type: 'checkbox', fixed: 'left'},
                            {field: 'id', title: 'ID', fixed: 'left', unresize: true, sort: true},
                            {field: 'projectName', title: '项目名', edit: 'text'},
                            {field: 'demandTime', title: '需求日期', edit: 'text', sort: true},
                            {field: 'labelName', title: '标签', edit: 'text'},
                            {field: 'demandInfo', title: '需求详述', edit: 'text'},
                            {field: 'versionId', title: '版本号', edit: 'text', sort: true},
                            {
                                field: 'demandStatus', title: '状态',class:'asd', edit: 'text', sort: true,
                                templet: function (status) {
                                    if (status.demandStatus == 0) {
                                        return '<div class="layui-table-cell laytable-cell-1-0-6" style="padding-left: 0px;color: #e31405">未完成</div>'
                                    } else {
                                        return '<div class="layui-table-cell laytable-cell-1-0-6" style="padding-left: 0px;">已完成</div>'
                                    }
                                }
                            },
                            {field: 'userId', title: '录入人'},
                            {field: 'createTime', title: '创建时间', sort: true},
                        ]
                    ],
                // done: function (res, curr, count) {
                    //如果是异步请求数据方式，res即为你接口返回的信息。
                    //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
                    // console.log(res);
                    //得到当前页码
                    // console.log(curr);
                    //得到数据总量
                    // console.log(count);
            //         var that = this.elem.next();
            //         res.data.forEach(function (item, index) {
            //             if (item.demandStatus == 0) {
            //                 var tr = that.find(".layui-table-box tbody tr[data-index='" + index + "']");
            //                 let td = $(tr[0].children[6])
            //                 td.css("background-color", "#FF5722");
            //                 td.css("color", "white");
            //             }
            //         });
            // },
            page: true
        });
    })
    //监听单元格编辑
    table.on('edit(table)', function(obj){
        var value = obj.value //得到修改后的值
            ,data = obj.data //得到所在行所有键值
            ,field = obj.field; //得到字段
        console.log(obj)
        $.ajax({
            type: "POST",
            url: "/demand/edit",
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify(obj.data),
            dataType: "json",
            success: function (data) {
                if (data.code=200){
                    layer.msg('['+data.message+']' + field + ' 字段更改值为：'+ util.escape(value));
                }
            },
            error: function (data) {
            }
        });
        //
    });


    //监听提交
    form.on('submit(demo1)', function (data) {
        //获得右侧数据
        let getData = transfer.getData('transfer_data');
        let label_name = ''
        for (let checkData in getData) {
            label_name += "," + getData[checkData].title
        }
        let form_data = data.field;
        form_data.label_name = label_name.substr(1);
        //发异步
        $.ajax({
            type: "POST",
            url: "/demand/saveDemandData",
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify(form_data),
            dataType: "json",
            success: function (data) {
                if (data.code = 200) {
                    layer.msg("需求录入成功")
                    // 清空表单 (“addGoodsForm”是表单的id)
                    $("#addDemandForm")[0].reset();
                    layui.form.render();
                    // 提交表单数据清空
                    $.get('/demand/getLabelData', function (result) {
                        //显示搜索框
                        transfer.render({
                            elem: '#test4', //绑定元素
                            title: ['未选择', '已选择'],
                            showSearch: true,//开启搜索框
                            data: result.data,
                            id: 'transfer_data', //定义索引
                            parseData: function (res) {
                                return {
                                    "value": res.id ////穿梭框的唯一标识
                                    , "title": res.labelName //穿梭框的值
                                    // , "checked": '' //是否选中
                                }
                            },
                        })
                    })
                } else {
                    layer.msg("服务器正在快马加鞭为你处理中")
                }
            },
            error: function (data) {
            }
        });
        return false;
    });
    });
</script>
</body>
</html>